package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Train;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.TranQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ITrainService;
import cn.wolfcode.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("train")
@Controller
public class TrainController {
    @Autowired
    private ITrainService trainService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("view")
    public String list() {
        return "/train/train";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Train> query(EmployeeQueryObject qo)
    {
        return trainService.selectAll();
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(TranQueryObject qo)
    {
        return trainService.query(qo);
    }
    @RequestMapping("changeState")
    @ResponseBody
    public void changeState(Long id)
    {
        trainService.deleteByPrimaryKey(id);
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Train train)
    {
        try
        {
            if (train.getId() != null)
                trainService.updateByPrimaryKey(train);
            else
                trainService.insert(train);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return JSONResult.mark("保存失败!");
        }
        return JSONResult.success("保存成功!");
    }
    @RequestMapping("selectByEmployee")
    @ResponseBody
    public List<Employee> selectByEmployee()
    {
        return employeeService.selectAll();
    }
}
